package com.lagou.task15;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

public class TreeSetTest {
    public static void main(String[] args) {
        //准备一个TreeSet集合
        Set<String> s1 = new TreeSet<>();
        System.out.println("s1 = " +  s1);//s1 = []
        //向集合中添加String类型对象并打印
        boolean b1 = s1.add("a");
        System.out.println("b1 = " + b1);//b1 = true
        System.out.println("s1 = " +  s1);//s1 = [a]

        boolean b2 = s1.add("c");
        System.out.println("b1 = " + b2);//b1 = true
        System.out.println("s1 = " +  s1);//s1 = [a,c]

        boolean b3 = s1.add("b");
        System.out.println("b1 = " + b3);//b1 = true
        System.out.println("s1 = " +  s1);//s1 = [a,b,c]

        System.out.println("---------------------------------------");
        //准备一个集合存放student类型的对象
        Set<Student> stu1 = new TreeSet<>();
        stu1.add(new Student("g",35));
        stu1.add(new Student("l",40));
        stu1.add(new Student("z",30));
        System.out.println("stu1= " + stu1);

        System.out.println("----------------------------------------");
//        准备一个比较器对象作为参数传递给构造方法
        Comparator<Student> comparator = (Student o1,Student o2)->{return o1.getAge()-o2.getAge();};
//        Comparator<Student> comparator = new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o1.getAge() - o2.getAge();
//            }
//        };
        Set<Student> stu2 = new TreeSet<>();
        stu2.add(new Student("g",35));
        stu2.add(new Student("l",40));
        stu2.add(new Student("z",30));
        System.out.println("stu2= " + stu2);

    }
}
